#include<iostream>
//#include "opencv2/core/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <fstream>


using namespace std;

int main() {
    // 下载并处理图像
    const std::string img_path = "./imagenet_cat.png"; // 假设你已经下载了图像

    cv::Mat image = cv::imread(img_path);    
    // 检查图像是否成功读取
    if (image.empty()) {
        std::cerr << "Error: Unable to read the image." << std::endl;
        return -1;
    }
    
    // 调整大小为 224x224
    cv::resize(image, image, cv::Size(224, 224));
    // 获取图像的通道数
    int channels = image.channels();    
    image.convertTo(image, CV_32F);

    std::vector<float> mean = {0.485f, 0.456f, 0.406f};
    std::vector<float> stddev = {0.229f, 0.224f, 0.225f};

    // 定义数组大小
    const int dim1 = 1;
    const int dim2 = 3;
    const int dim3 = image.rows;
    const int dim4 = image.cols;

    // 生成四维数组
    std::vector<std::vector<std::vector<std::vector<float>>>> fourDMatrix(
        dim1,
        std::vector<std::vector<std::vector<float>>>(
            dim2,
            std::vector<std::vector<float>>(
                dim3,
                std::vector<float>(dim4, 0.0f)
            )
        )
    );

    // 将图像数据复制到三维矩阵中
    for (int i = 0; i < image.rows; ++i) {
        for (int j = 0; j < image.cols; ++j) {
            for (int c = 0; c < 3; ++c) {
		if(c == 0)    
                    fourDMatrix[0][c][i][j] = (image.at<cv::Vec3f>(i, j)[2-c] / 255.0 - mean[0]) / stddev[0];
                if(c == 1)
                    fourDMatrix[0][c][i][j] = (image.at<cv::Vec3f>(i, j)[2-c] / 255.0 - mean[1]) / stddev[1];
                if(c == 2)
		
    		    fourDMatrix[0][c][i][j] = (image.at<cv::Vec3f>(i, j)[2-c] / 255.0 - mean[2]) / stddev[2];	    	    
                 }
        }
    }
    
    // 输出三维矩阵的内容
    for (int i = 0; i < image.rows; ++i) {
        for (int j = 0; j < image.cols; ++j) {
            for (int c = 0; c < 3; ++c) {
               std::cout << fourDMatrix[0][c][i][j] << " ";
            }
            std::cout << "| ";
        }
        std::cout << std::endl;
    }
            
    std::cout << std::endl;
    std::cout << fourDMatrix.size() << endl;
    std::cout << fourDMatrix[0].size() << endl;
    std::cout << fourDMatrix[0][0].size() << endl;
    std::cout << fourDMatrix[0][0][0].size() << endl;
    std::cout << image.rows << " * " << image.cols << endl;
    
    /*
    cv::FileStorage fs("imagenet_cat.npz", cv::FileStorage::WRITE);
    fs << "data" << img_data_batched;
    fs.release();
    */    
    return 0;
}
